package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnTransactionBeginNode extends Shape {

	public static final String KEY_NODE_TRANSATIONBEGIN = "transactionBegin";
	public static final String SHOW_NODE_TRANSATIONBEGIN = Messages
			.getString("SaturnTransactionBeginNode.StartBusiness"); //$NON-NLS-1$

	public static final String KEY_PROPERTY_TRANSATIONBEGIN_NAME = "_property_name";

	public static final String KEY_PROPERTY_TRANSATIONBEGIN_DESCRIPTION = "_property_description";

	public static final String SHOW_NODE_TRANSATIONBEGIN_NAME = "name";

	public static final String SHOW_NODE_TRANSATIONBEGIN_DESCRIPTION = "description";

	private static final long serialVersionUID = -8472801299160796077L;

	private static int number = 1;

	private Image image = Activator.getImage(Images.TRANSATIONBEGIN_ICON);

	public SaturnTransactionBeginNode() {
		super.setName(SHOW_NODE_TRANSATIONBEGIN + number++);
	}

	@Override
	public IFigure getFigure() {

		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	@Override
	public Image getIcon() {
		return image;

	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node transactionBeginNode = SaturnW3CUtil.createChildNode(root,
				KEY_NODE_TRANSATIONBEGIN, null);

		SaturnW3CUtil.createChildNode(transactionBeginNode,
				SHOW_NODE_TRANSATIONBEGIN_NAME, super.getName());

		return transactionBeginNode;
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnTransactionBeginNode transactionNode = new SaturnTransactionBeginNode();
		SaturnTransactionBeginNode saturn = (SaturnTransactionBeginNode) shape;
		transactionNode.setLocation(mousePoint);
		transactionNode.setName(saturn.getName());
		transactionNode.setDescription(saturn.getDescription());
		transactionNode.setId(saturn.getId());
		transactionNode.setNextId(saturn.getNextId());
		return transactionNode;
	}

	@Override
	protected void setImage(Image image) {
		this.image = image;
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();
		
		infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("description", getDescription());
		
		para.setInfourl( name + "-transactionBegin.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.TransactionStartNode")); //$NON-NLS-1$
		para.setImagename("transactionBegin");
		String templatePath = "template/export/logic/transactionbegin";
		String Path = filePath + File.separator + name + "-transactionBegin.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,infos, Path);
		
		return para;
	}
}
